//
//  GetBackPwdViewController.m
//  WGFGlobalHouse
//
//  Created by waiguofang on 15/4/15.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//   找回密码

#import "GetBackPwdViewController.h"
#import "LoginDataMange.h"
#import "MBProgressHUD+MJ.h"
#import "UserModal.h"
#import "UIView+circular.h"
#import "SetPwdViewController.h"
#import "config.h"
#import "StringTool.h"


@interface GetBackPwdViewController ()<UITextFieldDelegate,UIScrollViewDelegate>
- (IBAction)tapSaveInfoBtn;


@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *yanZhengCodeTextFiled;
@property (strong, nonatomic) LoginDataMange *dataMange;

@property (weak, nonatomic) IBOutlet UIView *backView;

//获取验证码
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation GetBackPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBarHidden = NO;
    
   
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    [self setTextFiled];
    
    if (self.type == isGetBackPwdVC)
    {
        self.codeBtn.hidden = NO;
        self.codeBtn.enabled = YES;
    }else
    {
        self.codeBtn.hidden = YES;
        self.codeBtn.enabled = NO;
    }
    
}



-(void)setTextFiled
{
    [self.titleView setMianTitle:@"找回密码"];
    self.titleView.doneBtn.hidden = YES;
    [self.loginBtn viewToCircular];
    [self.codeBtn viewToCircular];
   
    self.dataMange = [[LoginDataMange alloc]init];
    
    UITapGestureRecognizer *recog = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(test)];
    [self.backView addGestureRecognizer:recog];
    
}

-(void)test
{
    NSLog(@"====test====");
//    [self.backView endEditing:YES];
    [self.view endEditing:YES];
//    [self.mainView endEditing:YES];
}

#pragma mark 点击了“下一步”，或者“保存信息”按钮
- (IBAction)tapSaveInfoBtn;
{
    
    if ([self checkPhone]) {
        
        SetPwdViewController *vc = [[SetPwdViewController alloc]initWithNibName:@"SetPwdViewController" bundle:nil];
        vc.phoneStr = self.phoneTextField.text;
        vc.captchaStr = self.yanZhengCodeTextFiled.text;
         vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark 校验手机号
-(BOOL)checkPhone
{
    if ([StringTool isBlankString:self.phoneTextField.text]) {
        [MBProgressHUD showError:@"请输入手机号"];
        return NO;
    }else if (![self validateMobile:self.phoneTextField.text])
    {
        [MBProgressHUD showError:@"请输入正确的手机号"];
        return NO;
    }else if ([StringTool isBlankString:self.yanZhengCodeTextFiled.text])
    {
        [MBProgressHUD showError:@"请输入验证码"];
        return NO;
    }
    return YES;
}
#pragma mark 点击了获取验证码按钮
- (IBAction)clickGetCodeBtn:(id)sender {
    
    if (self.type == isGetBackPwdVC)
    {
        if ([self.phoneTextField.text length] == 0)
        {
            [MBProgressHUD showError:@"请输入手机号码"];
        }
        else
        {
            
            
            BOOL validata =  [self validateMobile:self.phoneTextField.text];
            if (validata == YES)
            {
                
                [self getCode];
                
            }else
            {
                [MBProgressHUD showMessage:@"请输入正确的手机号!"];
            }
        }
        
    }

}


#pragma mark 获取的验证的请求
-(void)getCode
{
    [self Countdown];
    
    __weak typeof(self)bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        ResulteMod *mod =  [bself.dataMange captchaWithMobile:bself.phoneTextField.text];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            if (mod.resltState == ResultFail) {
                [MBProgressHUD showError:mod.resultTip];
            }else if (mod.resltState == ResultNetError)
            {
                [MBProgressHUD showError:@"网络错误"];
            }else
            {
                [MBProgressHUD showSuccess:@"验证码已发送，注意查收"];
            }
            
        });
        
    });

}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    
    CGRect dd =  [textField.superview convertRect:textField.frame toView:self.backView];
    
    int tvbotY = dd.size.height+dd.origin.y;
    int mid  = PMH-[UserModal shareUserModel].keyboardHeight-tvbotY -64;
    
    if (mid<0) {
        
        const float movementDuration = 0.3f;
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        
        CGRect buf = self.backView.frame;
        buf.origin.y = up ? mid : 0;
        [self.backView setFrame:buf];
        
        [UIView commitAnimations];
    }else if(up==false){
        
        [self viewRest];
    }
}



-(void)viewRest{
    
    
    const float movementDuration = 0.3f;
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    
    CGRect buf = self.backView.frame;
    buf.origin.y =  0;
    [self.backView setFrame:buf];
    
    [UIView commitAnimations];
}

#pragma et delegare
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [self animateTextField:textField up:false];
    
    [textField resignFirstResponder];
    
    [self viewRest];
    return  YES;
}

#pragma mark 验证码倒计时
-(void)Countdown
{
    __block NSInteger timeout = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if (timeout <=0) {
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.codeBtn setTitle:[NSString stringWithFormat:@"获取验证码"] forState:UIControlStateNormal];
                self.codeBtn.enabled = YES;
            });
            
        }else
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.codeBtn setTitle:[NSString stringWithFormat:@"%zd重新获取",timeout] forState:UIControlStateNormal];
                self.codeBtn.enabled = NO;
        
            });
            timeout--;
        }
    });
    dispatch_resume(timer);
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    [super touchesEnded:touches withEvent:event];
    
    
    NSLog(@"-----eeee");
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
{
    NSLog(@"---eeee--eeee");

}

@end
